﻿using Resn.WhuPub.DML;
using System.Collections.Generic;
using System.Linq;

namespace Resn.WhuPub.DAL
{
        /// <summary>
        /// 提供Guid唯一标识的对象
        /// </summary>
        public static class DBCheck
        {
                /// <summary>
                /// 判断数据库中是否存在此标识的对象
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <returns>存在则返回True，否则返回False</returns>
                public static bool ExistId<T>(string id) where T : class, IIdentity
                {
                        dynamic t = DBStorage.Instance.Select<T>(id);
                        if (t == null)
                        {
                                return false;
                        }
                        return true;
                }


                /// <summary>
                /// 判断集合中是否存在Id属性值与比较值相同的对象
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <param name="collection">集合</param>
                /// <param name="id">比较值</param>
                /// <returns>存在则返回True，否则返回False</returns>
                public static bool ExistId<T>(List<T> collection, string id) where T : class,IIdentity
                {
                        return collection.Any(i => ((dynamic)i).Id == id);
                }

                /// <summary>
                /// 判断集合中是否存在Name属性值与比较值相同的对象
                /// </summary>
                /// <typeparam name="T"></typeparam>
                /// <param name="collection">集合</param>
                /// <param name="name">比较值</param>
                /// <returns>存在则返回True，否则返回False</returns>
                public static bool ExistName<T>(List<T> collection, string name) where T : class,IIdentity
                {
                        return collection.Any(i => ((dynamic)i).Name == name); 
                }
        }
}